package com.jerry.reflect;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.FileNameMap;
import java.net.SocketTimeoutException;

/**
 * Created by Shirly on 2016/12/5.
 */
public class test2 {
    public static void main(String[] args) {
        try {
//            Class a = Class.forName("com.jerry.reflect.Axe");
//            Object axe = a.newInstance();
//            Field f = a.getDeclaredField("id");
//            f.setAccessible(true);
//            f.set(axe, "123");

            Class a = Class.forName("com.jerry.reflect.Axe");
            Object b = a.newInstance();
            Field field = a.getDeclaredField("name");
            field.setAccessible(true);
            field.set(b,"xiaoming");
            System.out.println(field.get(b));
            if(b instanceof Axe){
                Axe axe = (Axe)b;
                System.out.println(axe.getName());
            }

//            System.out.println(f.getName());
//            System.out.println(f.get(axe));
//            System.out.println(a.getSuperclass().getSimpleName());
//            System.out.println(a.getInterfaces());
            System.out.println(a.getDeclaredConstructors().getClass().getSimpleName());
            System.out.println(a.getDeclaredMethods());
            System.out.println(a.getDeclaredMethod("use").getClass().getSimpleName());
            System.out.println(a.getDeclaredMethod("use").getClass().getName());
            System.out.println(a.getDeclaredMethod("use").getName());
            System.out.println(a.getDeclaredMethod("use"));
            System.out.println(a.getDeclaredMethods().getClass().getSimpleName());
            System.out.println("获取方法的返回类型："+a.getDeclaredMethod("use").getReturnType());
            System.out.println("获取方法的返回类型："+a.getDeclaredMethod("abx",String.class,int.class).getReturnType());
            System.out.println("获取方法的参数类型："+a.getDeclaredMethod("abx",String.class,int.class).getParameterTypes());
            System.out.println("------------------------------------");
            Method[] methods  = a.getDeclaredMethods();
            for (Method method :methods){
                System.out.println("获取方法的返回类型:"+method.getReturnType());
                System.out.println("获取方法的参数类型："+method.getParameterTypes());
                System.out.println(method.getGenericParameterTypes());
               Class[] paramType = method.getParameterTypes();
                for (Class c :paramType){
                    System.out.println("获取的参数类型是："+c.getName());
                }
            }
//            System.out.println(a.getDeclaredConstructor(String.class));
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }
}
